import lawTopBar from "@/components/lawTopBar/index.vue";
import { getAcctList, getSortList } from "@/static/js/law.js";
import { hightLightText } from "@/utils/utils";
export default {
  components: {
    lawTopBar,
  },
  data() {
    return {
      title: "会计法规库",
      backgroundUrl: require("@/static/img/law/topBar_bg_02.png"),
      searchText: "",
      scrollTop: 0,
      sortList: [
        {
          id: 1,
          nid: 1,
          name: "热点法规",
        },
        {
          id: 2,
          nid: 2,
          name: "中央法规",
        },
        {
          id: 3,
          nid: 3,
          name: "地方法规",
        },
        {
          id: 27,
          name: "海关综合文件",
        },
        {
          id: 20,
          name: "国际跨境税收",
        },
        {
          id: 28,
          name: "外汇文件",
        },
        {
          id: 32,
          name: " 12366热点问答",
        },
        {
          nid: 4,
          name: "法规法条",
        },
      ], //分类
      chooseTagId: 1,
      keyword: "",
      form: {
        id: "",
        page: 1,
        pagesize: 10,
        keyword: "", //	否	string	搜索字段
      },
      isLoading: false,
      loadMoreStatue: "more",
      count: 0,
      lawList: [],
      isRefreshing: false,
      loadMoreStatus: "more",
      chooseLevel1TypeName: "",
      levelList: [],
      level2List: [],
      chooseLevel1Index: 0,
      isSearch: false,
    };
  },
  methods: {
    hightLightText(text, keyword) {
      return hightLightText(text, keyword);
    },
    goSearch() {
      uni.navigateTo({
        url: "../search/index",
      });
    },

    //二级分类切换
    chooseTag(e) {
      this.chooseTagId = e.id;
      this.form.id = e.id;
      this.form.page = 1;
      this.lawList = [];
      this.getList();
    },

    //请求列表数据
    getList() {
      var that = this;
      if (this.form.page == 1) {
        this.lawList = [];
        this.isLoading = true;
      } else {
        this.loadMoreStatue = "loading";
      }
      getAcctList(this.form).then((res) => {
        if (res.code == 1) {
          if (res.data.length > 0) {
            this.count = res.data[0].datacount;
          } else {
            this.count = 0;
          }
          var newList = res.data; //本次请求获取到的新数据
          //如果新数据的数量达到单页数量,则认为还有更多数据
          if (newList.length >= that.form.pagesize) {
            that.loadMoreStatus = "more";
          } else {
            that.loadMoreStatus = "noMore";
          }
          this.lawList = [...this.lawList, ...newList];
          this.isRefreshing = false;
        } else {
          this.$tools.showToast(res.msg);
          this.loadMoreStatus = "noMore";
        }
      });
    },

    //下拉刷新
    reload() {
      this.form.page = 1;
      this.lawList = [];
      this.isRefreshing = true;
      this.getList();
    },

    //加载更多
    loadMore() {
      if (this.loadMoreStatus == "more") {
        this.form.page++;
        this.getList();
      } else {
        this.$tools.msg("没有更多了");
      }
    },

    /**获取会计法规分类**/
    getSortList() {
      this.isLoading = true;
      getSortList().then((res) => {
        if (res.code == 1) {
          let data = res.data;
          for (let key in data) {
            this.levelList.push({
              id: data[key].oneclass.id,
              name: data[key].oneclass.name,
              level2List: data[key].twoclass,
            });
          }
          let id = this.levelList[0].level2List[0].id;
          this.chooseLevel1TypeName = this.levelList[0].name;
          this.level2List = this.levelList[0].level2List;
          this.chooseTagId = id;
          this.form.id = id;
          this.getList();
        }
      });
    },
    //切换一级分类
    chooseLevel(e) {
      console.log(e.detail.value);
      // 选中的一级分类下标
      let index = e.detail.value;
      this.chooseLevel1Index = index;
      this.chooseLevel1TypeName = this.levelList[index].name;
      // 默认选中二级分类第一个
      let id = this.levelList[index].level2List[0].id;

      //选中一级分类后的二级分类列表
      this.level2List = this.levelList[index].level2List;
      this.chooseTagId = id;
      this.form.id = id;
      this.lawList = [];
      this.form.page = 1;
      this.getList();
    },

    LevelCancel() {},
    //去详情
    toInfo(id) {
      uni.navigateTo({
        url: "../det/index?id=" + id,
      });
    },
  },
  onReady() {},
  onShow() {
    this.getSortList();
  },
};